#include <stdlib.h>
#include <stdio.h>
#include "sprite.h"


extern int HRES, VRES;

char *read_xpm(char *map[], int *wd, int *ht) {
    __attribute__((unused)) static char read_xpm_jcard;

    int width, height, colors;
    char sym[256];
    int col;
    int i, j;
    char *pix, *pixtmp, *tmp, *line;
    char symbol;

    // read width, height, colors
    if (sscanf(map[0], "%d %d %d", &width, &height, &colors) != 3) {
        printf("read_xpm: incorrect width, height, colors\n");
        return NULL;
    }
#ifdef DEBUG
    printf("%d %d %d\n", width, height, colors);
#endif
    if (width > HRES || height > VRES || colors > 256) {
        printf("read_xpm: incorrect width, height, colors\n");
        return NULL;
    }

    *wd = width;
    *ht = height;

    for (i = 0; i < 256; i++)
        sym[i] = 0;

    // read symbols <-> colors
    for (i = 0; i < colors; i++) {
        if (sscanf(map[i + 1], "%c %d", &symbol, &col) != 2) {
            printf("read_xpm: incorrect symbol, color at line %d\n", i + 1);
            return NULL;
        }
#ifdef DEBUG
        printf("%c %d\n", symbol, col);
#endif
        if (col > 256) {
            printf("read_xpm: incorrect color at line %d\n", i + 1);
            return NULL;
        }
        sym[col] = symbol;
    }

    // allocate pixmap memory
    pix = pixtmp = malloc(width * height);

    // parse each pixmap symbol line
    for (i = 0; i < height; i++) {
        line = map[colors + 1 + i];
#ifdef DEBUG
        printf("\nparsing %s\n", line);
#endif
        for (j = 0; j < width; j++) {
            tmp = memchr(sym, line[j], 256); // find color of each symbol
            if (tmp == NULL) {
                printf("read_xpm: incorrect symbol at line %d, col %d\n", colors + i + 1, j);
                return NULL;
            }
            *pixtmp++ = tmp - sym; // pointer arithmetic! back to books :-)
#ifdef DEBUG
            printf("%c:%d ", line[j], tmp - sym);
#endif
        }
    }

    return pix;
}

Sprite * create_sprite(char *pic[], char *base) {
    int wd, ht;
    char* spr = read_xpm(pic, &wd, &ht);

    Sprite* s = (Sprite *) malloc(sizeof (Sprite));

    if (s == NULL)
        return NULL;

    if ((s->map = spr) == NULL) {
        free(s);
        return NULL;
    }

    s->x = 0;
    s->y = 0;
    s->width = wd;
    s->height = ht;
    s->xspeed = 0;
    s->yspeed = 0;
    s->map = spr;
    return s;
}

void draw_sprite(Sprite *sprt, char *base) {
    //char *dMap=base+sprt->y*HRES+sprt->x;
    char *dSprt = sprt->map;
    int i, k;
    for (i = 0; i < sprt->height; i++)
        for (k = 0; k < sprt->width; k++) {
            if (*dSprt != 0)
                set_pixel(sprt->x + k, sprt->y + i, *dSprt, base);
            //*(dMap + i*HRES+k)=*dSprt;
            dSprt++;
        }
}

void destroy_sprite(Sprite *sprt, char *base) {
    if (sprt == NULL)
        return;


    free(sprt->map);
    free(sprt);
    sprt = NULL;
}

void set_init(Sprite *sprt, char *base) {

    sprt->x = 608;
    sprt->y = 495;
    sprt->yspeed = 2;

}

void set_init_level3(Sprite *sprt, char *base) {

    sprt->x = 608;
    sprt->y = 495;
    sprt->yspeed = 1;

}

void set_init_level4(Sprite *sprt, char *base) {

    sprt->x = 450;
    sprt->y = 495;
    sprt->yspeed = 1;

}


void set_init_getready(Sprite *sprt, char *base) {

    sprt->x = 205;
    sprt->y = 225;
  

}

void set_init_contagem(Sprite *sprt, char *base) {

    sprt->x = 310;
    sprt->y = 225;
    

}
/*
int animate_sprite(Sprite *fig, char *base)
{
        fig->x += fig->xspeed;
        fig->y += fig->yspeed;
        draw_sprite(fig, base);
        delay(1);
}
 */

/*
void clean_sprite(Sprite *sprt, char *base){
        int height = sprt->height, width = sprt->width;
        int i,k;
	
        for(i=0; i < height; i++)
                for(k=0; k < width; k++)
                {
                        set_pixel(sprt->x+k,sprt->y+i, *(sprt->fundo + sprt->width*i + k), base);  //desenha o fundo guardado no local da sprite
                }
	
        sprt->valid = false;
	
}

void move_sprite(Sprite *sprt, int x, int y, char *base, char *dest)
{
        //memcpy(base, dest,HRES*VRES*sizeof(char));
        clean_sprite(sprt, dest);
        sprt->x = x;
        sprt->y = y;
        draw_sprite(sprt, dest);
	
        memcpy(base, dest,HRES*VRES*sizeof(char));
}

 */

